package com.ki.rent.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ki.rent.entity.Admin;
import com.ki.rent.entity.Booking;
import com.ki.rent.entity.House;
import com.ki.rent.entity.User;
import com.ki.rent.entity.UserData;
import com.ki.rent.service.AdminService;
import com.ki.rent.service.BookingService;
import com.ki.rent.service.HouseService;
import com.ki.rent.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 */
@Controller
@RequestMapping("/rent/admin")
public class AdminController {

    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private HouseService houseService;

    @Autowired
    private BookingService bookingService;

    @GetMapping ("/adminLogin")
    public String adminLogin(@RequestParam("account") String account,
                             @RequestParam("password") String password,
                             Model model, HttpSession session){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        List<Admin> admins = adminService.list(wrapper);
        for (Admin admin : admins) {
            if ((admin.getAccount()).equals(account) && (admin.getPassword()).equals(password)){
                model.addAttribute("admin",admin);
                session.setAttribute("admin", admin);
                return "admin/adminindex";
            }
        }
        model.addAttribute("msg","管理员账号或密码错误");
        return "login";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/allUser")
    public String all(Model model){
        List<User> lists = userService.list(null);
        model.addAttribute("lists",lists);
        return "admin/all";
    }

    @RequestMapping("/toUpdate/{userId}")
    public String toUpdate(@PathVariable("userId") String userId,Model model){
        User user = userService.getById(userId);
        model.addAttribute("user",user);
        return "admin/adminupdate";
    }

    //去更新管理员密码页面
    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd(HttpServletRequest request) {
        return "admin/updatepwd";
    }

    //更新密码
    @ResponseBody
    @RequestMapping("/updatePwd")
    public String updatePwd(String oldpwd, String password, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (!admin.getPassword().equals(oldpwd)) {
            return "FAIL";
        }
        admin.setPassword(password);
        boolean b = adminService.saveOrUpdate(admin);
        if (b) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

    @RequestMapping("/update")
    public String update(User user){
        userService.updateById(user);
        return "redirect:/rent/admin/allUser";
    }

    @RequestMapping("/delete/{userId}")
    public String delete(@PathVariable("userId") Integer userId,Model model){
        userService.removeById(userId);
        return "redirect:/rent/admin/allUser";
    }


    @RequestMapping("/allHouse")
    public String allHouse( Model model){
        List<House> houses = houseService.list(null);
        model.addAttribute("houses",houses);
        int size = houses.size();
        model.addAttribute("size",size);
        return "house/allhouse";
    }
    @RequestMapping("/house")
    public String house( Model model){
        List<House> houses = houseService.list(null);
        model.addAttribute("houses",houses);
        int size = houses.size();
        model.addAttribute("size",size);
        return "house/myhouse";
    }

  // 已租出房源
    @RequestMapping("/outHouse")
    public String outHouse(Model model, HttpServletRequest request) {
      QueryWrapper<House> wrapper = new QueryWrapper<>();
      wrapper.eq("house_still", 1);
      List<House> houses = houseService.list(wrapper);
      model.addAttribute("houses", houses);
      return "house/outhouse";
    }

    @RequestMapping("/inHouse")
    public String inHouse(Model model, HttpServletRequest request){
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("house_still",0);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        return "house/inhouse";
    }

    @RequestMapping("/allBooking")
    public String allBooking(Model model){
        List<Booking> bookings = bookingService.list(null);
        model.addAttribute("bookings",bookings);
        return "booking/hostbooking";
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public UserData getUser(){
        List<User> users = userService.list(null);
        UserData u = new UserData();
        u.setCode(0);
        u.setCount(users.size());
        u.setData(users);
        u.setMsg("OK");
        return u;
    }
}

